

    const userDao = require('../dao/userDao')
    const encryption = require('../lib/encryption')
    const Sequelize = require('sequelize')
    const Op = Sequelize.Op

    module.exports = {

        checkUser: async (username, password) => {
            // let userDataPs = await userDao.checklogin(username, password);
            let userData = {}
            password = await encryption.getMd5Pass(password)
            userData.userDataPs = await userDao.sequelizelogin(username, password)
            userData.username = username
            return userData.userDataPs.data
        },

        getUserData: async () => {
            return await userDao.returnUserInfo()
        },

        getRoleInfo: async () => {
            // let data = await userDao.returnRoleInfo()
            // console.log(data)
            return await userDao.returnRoleInfo()
        },

        showUserdata:  async () => {
            let UserInfo = await userDao.returnUserInfo()
            let RoleInfo = await userDao.returnRoleInfo()
            let showData = []
            for(let i=0;i<UserInfo.length;i++) {
                let obj = {}
                obj.id = UserInfo[i].id
                obj.username = UserInfo[i].username
                obj.phone = UserInfo[i].phone
                for(let j=0;j<RoleInfo.length;j++) {
                    if(RoleInfo[j].roleid === UserInfo[i].roleid) {
                        obj.rolename = RoleInfo[j].rolename
                    }
                }
                showData.push(obj)
            }
            return showData
        },

        searchUserData: async (searchInfo) => {
            let res = []
            let RoleInfo = await userDao.returnRoleInfo()
            const likeQuery = ['username', 'phone']
            for(let key in searchInfo) {
                if(searchInfo[key] === '' || searchInfo[key] === undefined) delete searchInfo[key]
                else {
                    if(likeQuery.includes(key)) {
                        searchInfo[key] = {
                            [Op.like]: `%${searchInfo[key]}%`
                        }
                    }
                }
            }
            let searchResult = await userDao.searchUserData(searchInfo);
            for(let i=0;i<searchResult.length;i++) {
                let oneRes = {}
                oneRes.username = searchResult[i].dataValues.username
                oneRes.phone = searchResult[i].dataValues.phone
                for(let j=0;j<RoleInfo.length;j++) {
                    if(RoleInfo[j].roleid === searchResult[i].dataValues.roleid) {
                        oneRes.rolename = RoleInfo[j].rolename
                    }
                }
                res.push(oneRes)
            }
            return res
        },

        addUser: async (name, phone, pass, roleid) => {
            pass = await encryption.getMd5Pass(pass)
            let userinfo = {
                username: name,
                phone: phone,
                pass: pass,
                roleid: roleid
            }
            return await userDao.addUser(userinfo)
        },

        deleteUser: async (username) => {
            await userDao.deleteUser(username)
        }
        
    }